package com.itheima.springboot_init;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    //      读取配置内容
//    1.获取数据value注入
    @Value("${name}")
    private String name;

    @Value("${person2.name}")
    private String name2;

    @Value("${person2.age}")
    private String age2;

    @Value("${address[0]}")
    private String add1;

    @Value("${msg1}")
    private String msg1;

    @Value("${msg2}")
    private String msg2;

    //    2.获取数据Autowired注入
    @Autowired
    private Environment env;

    //    3.获取数据先写好实体类，用@ConfigurationProperties注解
    @Autowired
    private Person person;


    @RequestMapping("/hello")
    public String Hello() {
        System.out.println("value注入:");
        System.out.println(name);
        System.out.println(name2);
        System.out.println(age2);
        System.out.println(add1);
        System.out.println(msg1);
        System.out.println(msg2);

        System.out.println("~~~~~~~~~~~~~~~~~~~~~(●'◡'●)~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("autowired注入:");

        System.out.println(env.getProperty("name"));
        System.out.println(env.getProperty("person.name"));
        System.out.println(env.getProperty("address[1]"));
        System.out.println(env.getProperty("msg2"));

        System.out.println("~~~~~~~~~~~~~~~~~~~~~(￣▽￣)'~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(person.getName());
        System.out.println(person.getAge());
        System.out.println(person);
        String[] address = person.getAddress();
        for (String s : address) {
            System.out.println(s);
        }
        return "hello,bitches";
    }


    @RequestMapping("/sucker")
    public String Sucker() {
        return "hello,bitches";
    }
}
